//
//  AppDelegate.swift
//  weibo
//
//  Created by afan on 16/3/25.
//  Copyright © 2016年 阿范. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        /// 设置导航栏皮肤
        setAppearance()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        
//        let VC = AfanNewFeatureViewController()
//        window?.rootViewController = AfanMainViewontroller()
        window?.rootViewController = defaultViewController()
        
        window?.makeKeyAndVisible()
        
        return true
    }
    
    private func defaultViewController() -> UIViewController {
        if !AfanUserAccountViewModel.sharedInstance.userLogin{
            return AfanMainViewontroller()
        }
        return isNewVersion() ? AfanNewFeatureViewController() : AfanWelcomeController()
    }
    
    func switchRootViewController(controller: UIViewController) {
        window?.rootViewController = controller
    }
    
    class func outSwithRootViewController(controller: UIViewController){
        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
        appDelegate?.switchRootViewController(controller)

    }
    
    
    
    /**
     是否是新版本
     
     - returns: 是否是新版本
     */
    private func isNewVersion() -> Bool {
        // 1.获取当前版本
        let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        print("version: \(version)")
        
        // 2.获取上一次的版本
        let sandboxKey = "sandboxKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxKey)
        
//        print("sandboxVersion: \(sandboxVersion)")
        
        // 3.对比, 不一样就是新版本
        let newVersion = version != sandboxVersion
        
        // 4.是新版本,保存当前版本号
        if newVersion {
            NSUserDefaults.standardUserDefaults().setObject(version, forKey: sandboxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        // 5.返回比较结果
        return newVersion
    }

    
    
    /// 设置皮肤
    private func setAppearance() {
        // 设置导航栏皮肤
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }

}

